{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7cdb3b0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['S__:4370=quriquriquriquripE',\n",
       " 'S__:1872=7488748874887488EP',\n",
       " 'S__:3782=15129512951295128E',\n",
       " 'S__:3436=qeurteurteurteurrE',\n",
       " 'S__:2659=qpyeupyeupyeupyeyE',\n",
       " 'S__:8508=erpetrpetrpetrpewE',\n",
       " 'S__:6202=wriqpriqpriqpripiE',\n",
       " 'S__:7528=叁零壹壹伍零壹壹伍零壹壹伍零壹壹贰E',\n",
       " 'S__:9819=三九二七九九二七九九二七九九二七六E',\n",
       " 'S__:7813=eqwttqwttqwttqwtwE']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%run common.ipynb\n",
    "\n",
    "[tokenizer.decode(i) for i in tokenizer.get_batch_data(prefix=False)[1]][:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5e45b488",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "model_gen = ModelGEN()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fbf00a68",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "S__:4952=肆五二8E\n",
      "S__:4761=九EPPP\n",
      "1000\n",
      "S__:5948=贰壹柒玖肆壹柒玖肆壹柒玖伍壹柒玖贰E\n",
      "S__:5106=0042604460426044oE\n",
      "2000\n",
      "S__:3943=1573357三三57335732E\n",
      "S__:8711=三四八四七四八四七四八四七四八四四E\n",
      "3000\n",
      "S__:7605=eprweprweprweprwpE\n",
      "S__:9670=三八六八三八六八三八六八三八六八〇E\n",
      "4000\n",
      "S__:1748=7992799279927992EP\n",
      "S__:7750=贰捌陆零贰捌陆零贰捌陆零贰捌陆零零E\n",
      "5000\n",
      "S__:9917=39671967196719668E\n",
      "S__:8157=叁贰陆叁壹贰陆叁壹贰陆叁壹贰陆贰捌E\n",
      "6000\n",
      "S__:1532=六一二八六一二八六一二八六一二八EP\n",
      "S__:8413=三三六五五三六五五三六五五三六五二E\n",
      "7000\n",
      "S__:1487=toritoritoritoriEP\n",
      "S__:3048=一二一九三二一九三二一九三二一九二E\n",
      "8000\n",
      "S__:1813=柒贰伍贰柒贰伍贰柒贰伍贰柒贰伍贰E\n",
      "S__:1526=陆壹零肆陆壹零肆陆壹零肆陆壹零肆E\n",
      "9000\n",
      "S__:3136=一二五四五二五四五二五四五二五四四E\n",
      "S__:3200=12801280128012800E\n",
      "10000\n",
      "S__:3851=qtrpttrpttrpttrprE\n",
      "S__:2963=一一八五三一八五三一八五三一八五二E\n",
      "11000\n",
      "S__:2488=玖玖玖贰玖玖玖贰玖玖玖贰玖玖玖贰EP\n",
      "S__:4904=19617961796179616E\n",
      "12000\n",
      "S__:3908=15633567356335632E\n",
      "S__:5260=二一〇四二一〇四二一〇四二一〇四〇E\n",
      "13000\n",
      "S__:7537=三〇一五一〇一五一〇一五一〇一四八E\n",
      "S__:6896=贰柒伍捌陆柒伍捌陆柒伍捌陆柒伍捌肆E\n",
      "14000\n",
      "S__:7923=贰叁陆玖肆叁陆玖肆叁陆玖肆叁陆玖贰E\n",
      "S__:5067=贰零陆柒零零陆柒零零陆柒零零陆陆捌E\n"
     ]
    }
   ],
   "source": [
    "optimizer = torch.optim.AdamW(model_gen.parameters(), lr=1e-4)\n",
    "criterion = torch.nn.CrossEntropyLoss(ignore_index=tokenizer.encoder['P'])\n",
    "\n",
    "for epoch in range(15000):\n",
    "    _, input_ids, attention_mask = tokenizer.get_batch_data(prefix=False)\n",
    "    input_ids = torch.LongTensor(input_ids).to(device)\n",
    "    attention_mask = torch.LongTensor(attention_mask).to(device)\n",
    "\n",
    "    logits = model_gen(input_ids=input_ids, attention_mask=attention_mask)\n",
    "\n",
    "    loss = criterion(logits[:, :-1].flatten(end_dim=1),\n",
    "                     input_ids[:, 1:].flatten())\n",
    "\n",
    "    loss.backward()\n",
    "    optimizer.step()\n",
    "    optimizer.zero_grad()\n",
    "\n",
    "    if epoch % 1000 == 0:\n",
    "        print(epoch)\n",
    "        for i in generate(model_gen, input_ids[:2, :9]):\n",
    "            print(tokenizer.decode(i.tolist()))\n",
    "\n",
    "model_gen.to('cpu')\n",
    "torch.save(model_gen, 'gen.model')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:cuda117]",
   "language": "python",
   "name": "conda-env-cuda117-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
